using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GameEditor.Common;

namespace TaskSystemClient
{
    public partial class DatePicker : Form
    {
        public DatePicker()
        {
            InitializeComponent();
            this.timer_ForQuit.Enabled = false;
        }

        private Delegate<DateTime?> datePicked = delegate { };

        public Delegate<DateTime?> DatePicked
        {
            get { return datePicked; }
            set { datePicked = value; }
        }

        private DateTime? date;

        public DateTime? Date
        {
            get { return date; }
            set 
            {
                date = value;
                if (date.HasValue)
                    calander1.Value = date.Value;
            }
        }


        protected override void OnDeactivate(EventArgs e)
        {
            base.OnDeactivate(e);
            //Close();
            this.timer_ForQuit.Enabled = true;
        }

        private void OnDone(DateTime? date)
        {
            this.date = date;
            datePicked(date);
            this.timer_ForQuit.Enabled = true;
        }

        private void calander1_NullDateClicked(object sender, EventArgs e)
        {
            OnDone(null);
        }

        private void calander1_SelectionChanged(object sender, EventArgs e)
        {
            OnDone(calander1.Value);
        }

        private void DatePicker_Load(object sender, EventArgs e)
        {

            Location = Utilities.GetPopupPosition(Cursor.Position, Size);
        }

        private void timer_ForQuit_Tick(object sender, EventArgs e)
        {
            Close();
        }
    }
}